package persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSource {

	private static Connection conexion;
	private static int anidamiento;
	
	
	public static Connection getConexion(){
		
		if(conexion == null){
			
			String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			String url ="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_alumnos";
			String user = "sa";
			String password = "password";
			
			try {
				Class.forName(driver);
				conexion = DriverManager.getConnection(url, user, password);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}else{
			anidamiento ++;
		}
		return conexion;
	}
	
	
	public static void close(){
		
		if(anidamiento == 0){
			
			try {
				conexion.close();
				conexion = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		else
			anidamiento --;
	}
	
	
}
